#include "portmonitorwindows.h"

#include <windows.h>
#include <dbt.h>
#include <QtDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QCoreApplication>

const GUID PortMonitorWindows::GUID_DEVINTERFACE_LIST[] =
{
    // GUID_DEVINTERFACE_USB_DEVICE
    { 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } },
    // GUID_DEVINTERFACE_DISK
    //{ 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
    // GUID_DEVINTERFACE_HID,
    { 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
    // GUID_NDIS_LAN_CLASS
    //{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } },
    // GUID_DEVINTERFACE_COMPORT
    { 0x86e0d1e0, 0x8089, 0x11d0, { 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73 } },
    // GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR
    { 0x4D36E978, 0xE325, 0x11CE, { 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } },
    // GUID_DEVINTERFACE_PARALLEL
    { 0x97F76EF0, 0xF883, 0x11D0, { 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C } },
    // GUID_DEVINTERFACE_PARCLASS
    { 0x811FC6A5, 0xF728, 0x11D0, { 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1 } }
};

//////////////////////////
/// \brief PortMonitorWindows
PortMonitorWindows::PortMonitorWindows(QList<PortFilterInfo> filters, QObject *parent) :
    PortMonitor(filters, parent)
{
}

void PortMonitorWindows::onStartMonitor(void* win) {
    QCoreApplication::instance()->installNativeEventFilter(this);

    HDEVNOTIFY hDevNotify;
    DEV_BROADCAST_DEVICEINTERFACE NotifacationFiler;
    ZeroMemory(&NotifacationFiler,sizeof(DEV_BROADCAST_DEVICEINTERFACE));
    NotifacationFiler.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotifacationFiler.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

    for(int i=0;i<static_cast<int>(sizeof(GUID_DEVINTERFACE_LIST)/sizeof(GUID));i++) {
        NotifacationFiler.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];

        hDevNotify = RegisterDeviceNotification(reinterpret_cast<HANDLE>(win), &NotifacationFiler, DEVICE_NOTIFY_WINDOW_HANDLE);
        if(!hDevNotify) {
            DWORD Err = GetLastError();
            qDebug() << "Register failed: " << Err <<endl;
        }
    }
}

void PortMonitorWindows::onStopMonitor() {
    QCoreApplication::instance()->removeNativeEventFilter(this);
}

bool PortMonitorWindows::nativeEventFilter(const QByteArray &eventType, void *message, long *result) {
    Q_UNUSED(eventType)
    Q_UNUSED(result)
    MSG* msg = reinterpret_cast<MSG*>(message);
    DWORD msgType = msg->message;
    if(msgType==WM_DEVICECHANGE) {
        PDEV_BROADCAST_HDR lpdb = reinterpret_cast<PDEV_BROADCAST_HDR>(msg->lParam);
        switch (msg->wParam) {
        case DBT_DEVICEARRIVAL:
            if (lpdb->dbch_devicetype == DBT_DEVTYP_PORT) {
                PDEV_BROADCAST_PORT pDevInf = reinterpret_cast<PDEV_BROADCAST_PORT>(lpdb);
                QString portName = QString::fromWCharArray(pDevInf->dbcp_name, static_cast<int>(wcslen(pDevInf->dbcp_name)));

                // callback
                notifyPortArrival(portName);
            }
            break;
        case DBT_DEVICEREMOVECOMPLETE:
            if (lpdb->dbch_devicetype == DBT_DEVTYP_PORT) {
                PDEV_BROADCAST_PORT pDevInf = reinterpret_cast<PDEV_BROADCAST_PORT>(lpdb);
                QString portName = QString::fromWCharArray(pDevInf->dbcp_name, static_cast<int>(wcslen(pDevInf->dbcp_name)));

                // callback
                notifyPortRemoved(portName);
            }
            break;
        }
    }
    return false;
}
